Page({
    data: {
        orders: [],
        selectedAll: false,
        navState: 0,
        totalPrice: 0
    },
    bindchange(e) {

        let index = e.detail.current;
        this.setData({
            navState: index
        })
    },
    //点击导航
    navSwitch: function (e) {
        let index = e.currentTarget.dataset.index;
        this.setData({
            navState: index
        })
    },
    onLoad: function (options) {
        this.loadOrder();
    },
    loadOrder: function () {
        var orders = wx.getStorageSync('orders');
        var newOrders = [];
        var totalPrice = 0;
        var selectedAll = true;
        for (var i = 0; i < orders.length; i++) {
            var order = orders[i];
            if (order.selected) {
                totalPrice += order.price * order.num;
            } else {
                selectedAll = false;
            }
            newOrders.push(order);
        }
        wx.setStorageSync('orders', newOrders);
        this.setData({
            totalPrice: totalPrice,
            orders: newOrders,
            selectedAll: selectedAll
        });
    },
    checkboxChange: function (e) {
        var ids = e.detail.value;
        console.log(ids[0]);
        var orders = wx.getStorageSync('orders');
        var totalPrice = 0;
        var newOrders = [];
        for (var i = 0; i < orders.length; i++) {
            var order = orders[i];
            var flag = true;
            for (var j = 0; j < ids.length; j++) {
                if (order.id == ids[j]) {
                    totalPrice += order.price * order.num;
                    order.selected = true;
                    flag = false;
                }
            }
            if (flag) {
                order.selected = false;
            }
            newOrders.push(order);
        }
        wx.setStorageSync('orders', newOrders)
        this.loadOrder();
    },
    checkAll: function (e) {
        var orders = wx.getStorageSync('orders');
        console.log(e);
        var newOrders = [];
        var selectedAll = this.data.selectedAll;
        for (var i = 0; i < orders.length; i++) {
            var order = orders[i];
            if (selectedAll) {
                order.selected = false;
            } else {
                order.selected = true;
            }
            newOrders.push(order);
        }
        wx.setStorageSync('orders', newOrders)
        this.loadOrder();
    },
    buy: function (e) {
        var orders = wx.getStorageSync('orders');
        for (var i = 0; i < orders.length; i++) {
            var order = orders[i];
            var id = order.id;
        }
        // console.log(id);
        if(orders.length>0){
        wx.navigateTo({
            url: '../payPage/payPage?id=' + id,
        })
         }else{
       wx.showToast({
            title: '请先添加商品哦',
            duration: 2000
        });}
    },
    
    addOrders: function (e) {
        console.log(e)
        var id = e.currentTarget.id;
        console.log(e.id)
        var orders = wx.getStorageSync('orders');
        var newOrders = [];
        for (var i = 0; i < orders.length; i++) {
            var order = orders[i];

            if (order.id == id) {
                var num = order.num;
                order.num = num + 1;
            }
            newOrders.push(order);
        }
        wx.setStorageSync('orders', newOrders);
        this.loadOrder();
    },
    minusOrders: function (e) {
        console.log(e);
        var id = e.currentTarget.id;
        console.log(id)
        var orders = wx.getStorageSync('orders');
        var newOrders = [];
        var add = true;
        for (var i = 0; i < orders.length; i++) {
            var order = orders[i];
            if (order.id == id) {
                var count = order.num;
                if (count >= 2) {
                    order.num = count - 1;
                }
            }
            newOrders.push(order);
        }
        wx.setStorageSync('orders', newOrders);
        this.loadOrder();
    },
    clear: function (e) {
        // console.log(e);
        var orders = wx.getStorageSync('orders');
        var newOrders = [];
        wx.setStorageSync('orders', newOrders);
        this.loadOrder();
    },
})